package com.h2cloud.Enum;

import lombok.Getter;

/**
 * @Package: com.h2cloud.Enum
 * @ClassName: WeekDay
 * @Author: h2cloud
 * @CreateTime: 2022/5/27 12:02 下午
 * @Description:
 */
@Getter
class WeekDay {
	public static final WeekDay MONDAY;
	public static final WeekDay TUESDAY;
	public static final WeekDay WEDNESDAY;
	public static final WeekDay THURSDAY;
	public static final WeekDay FRIDAY;
	public static final WeekDay SATURDAY;
	public static final WeekDay SUNDAY;

	private static final WeekDay[] VALUES;

	static {
		MONDAY = new WeekDay(1, "星期一");
		TUESDAY = new WeekDay(2, "星期二");
		WEDNESDAY = new WeekDay(3, "星期三");
		THURSDAY = new WeekDay(4, "星期四");
		FRIDAY = new WeekDay(5, "星期五");
		SATURDAY = new WeekDay(6, "星期六");
		SUNDAY = new WeekDay(7, "星期日");
		VALUES = new WeekDay[]{
				MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
		};
	}

	private final Integer code;
	private final String desc;

	private WeekDay(Integer code, String desc) {
		this.code = code;
		this.desc = desc;
	}

	// 返回所有的对象
	public static WeekDay[] values() {
		return VALUES;
	}

	// 遍历对象，根据 code，返回 code对应的 desc
	public static String getDescByCode(Integer code) {
		WeekDay[] weekDays = WeekDay.values();
		for (WeekDay weekDay : weekDays) {
			if (weekDay.getCode().equals(code)) {
				return weekDay.getDesc();
			}
		}
		throw new IllegalArgumentException("Invalid Enum code:" + code);
	}

	/**
	 * 校验前端传入的code是否合法
	 *
	 * @param code
	 * @return
	 */
	public static WeekDay of(Integer code) {
		for (WeekDay weekDay : WeekDay.values()) {
			if (weekDay.code.equals(code)) {
				return weekDay;
			}
		}
		// 如果根据 code找不到对应的 WeekDay，说明 code范围不对，是非法的
		throw new IllegalArgumentException("Invalid Enum code:" + code);
	}
}
